home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / xdme_1.84_src.lha / XDME / Src / Var / VarsBases.c < prev   
Encoding:
C/C++ Source or Header  |  1994-12-22  |  6.2 KB  |  275 lines

  1. #define PATCH_AVL
  2. /******************************************************************************
  3.  
  4.     MODULE
  5.     varsbases.c
  6.  
  7.     DESCRIPTION
  8.     lowlevel Variable support for DME/XDME
  9.     (Basic List/AVL-Tree Functions)
  10.  
  11.  
  12.     NOTES
  13.     Either use AVLTREES or MLISTS
  14.  
  15.     For ALL commands (even to the searches) You must use
  16.     the address of a family, not the family itself
  17.     (that was done to simplify source-modification: just
  18.      replace 'List'/[M]LIST by 'Tree'/APTR, and go)
  19.  
  20.     BUGS
  21.     <none known>
  22.  
  23.     TODO
  24.     More homogene Interface - Use of an opaque type
  25.     (So that silly redefinition in other modules can go away)
  26.  
  27.     EXAMPLES
  28.     -/-
  29.  
  30.     SEE ALSO
  31.     vars.c
  32.  
  33.     INDEX
  34.  
  35.     HISTORY
  36.     16-11-94 major cleanup; important: GET does not duplicate any more
  37.  
  38. ******************************************************************************/
  39.  
  40. /*
  41. **  (C)Copyright 1992 by Bernd Noll for null/zero-soft
  42. **  All Rights Reserved
  43. **
  44. **  RCS Header: $Id: VarsBases.c 1.2 1994/09/20 11:14:01 b_noll Exp b_noll $
  45. **
  46. **  Basic Functions to work with families of DME-Variables
  47. **  Put together to start a little bit of modularisation
  48. **  in the sources.
  49. **
  50. */
  51.  
  52. /**************************************
  53.         Includes
  54. **************************************/
  55.  
  56.  
  57. #ifdef PATCH_AVL
  58. # include "AVL.h"
  59. # define  VBASE struct TreeNode *
  60. #else
  61. # define VBASE MLIST
  62. #endif
  63.  
  64. //#include "defs.h"
  65. #include "HL.h"
  66. #include "AVL.h"
  67. #include "xdme_base.h"
  68.  
  69. /**************************************
  70.       Interne Defines & Strukturen
  71. **************************************/
  72. #ifdef PATCH_AVL
  73.  
  74. /* this is exactly a struct HNode + Value field - so we can use HL as
  75.    soon as we have a carrier ... the remaining problem is Macrovars ... */
  76.  
  77. struct _VARS
  78. {
  79.     struct TreeNode Node;
  80.     UBYTE           *Name;
  81.     WORD        Flags;
  82.     struct HClass  *Class;
  83.     struct SList    Locks;
  84.     struct HNode   *Parent;
  85.  
  86.     UBYTE       *Str;
  87. };
  88. //#define Name          Node.tn_Name
  89.  
  90. int iAVL_Append (APTR x, APTR y, APTR z);
  91. int iAVL_Remove (APTR x, APTR y, APTR z);
  92. int iAVL_Find    (APTR x, APTR y, APTR z);
  93.  
  94. #define ANY_Find(t,n)   iAVL_Find(t,(APTR)NodeStringComparison, n)
  95. #define ANY_Add(t,n)    iAVL_Append(t,(APTR)NodeNodeComparison, n)
  96. #define ANY_Remove(t,n) iAVL_Remove(t,(APTR)NodeNodeComparison, n)
  97.  
  98. #else
  99.  
  100. typedef struct _VARS
  101. {
  102.     struct Node Node;
  103.     WORD        pad1;
  104.     UBYTE       *Str;
  105. };
  106.  
  107. #define Name        Node.ln_Name
  108. #define ANY_Find(l,n)   FindName(l,n)
  109. #define ANY_Add(t,n)    AddHead(l,n)
  110. #define ANY_Remove(t,n) Remove(n)
  111.  
  112. #endif /* (not) PATCH_AVL */
  113.  
  114. #define VARS  struct _VARS
  115.  
  116. #define PB_DISPOSE  1
  117. #define PB_SET        3
  118. #define VAR_Value   25
  119. struct PB_Msg { ULONG ID; ULONG Attr; APTR Value; };
  120. static const ULONG __DROP = PB_DISPOSE;
  121.  
  122. /**************************************
  123.        Exports
  124. **************************************/
  125. /* --- You can only use either LISTS or AVL-Trees!!! */
  126.  
  127. /* Vars Operations */
  128. Prototype void     DelAllVarsFromTree ( VBASE *base );
  129. Prototype void     DelVarFromTree     ( VBASE *base, UBYTE *name );
  130. Prototype UBYTE *GetVarFromTree     ( VBASE *base, UBYTE *name );
  131. Prototype void     SetVarIntoTree     ( VBASE *base, UBYTE *name, UBYTE *value );
  132.  
  133.  
  134.  
  135.  
  136.  
  137. #define VAR_DelAll   DelAllVarsFromTree
  138. #define VAR_DelNamed DelVarFromTree
  139. #define VAR_GetNamed GetVarFromTree
  140. #define VAR_SetNamed SetVarIntoTree
  141.  
  142.  
  143. /**************************************
  144.       Implementation
  145. **************************************/
  146.  
  147. static int VAR_Dispatcher (VARS *o, struct PB_Msg *m)
  148. {
  149.     switch (m->ID) {
  150.     case PB_DISPOSE:
  151.     if (o->Parent) ANY_Remove (o->Parent, &o->Node);
  152.     if (o->Name) free (o->Name);
  153.     if (o->Str)  free (o->Str);
  154.     free (o);
  155.     return 1;
  156.     case PB_SET:
  157.     if (m->Attr == VAR_Value) {
  158.         o->Str = strrep (o->Str, m->Value);
  159.         return 1;
  160.     } /* if */
  161.     break;
  162.     default: ;
  163.     // error ("VAR: undefined method called");
  164.     } /* switch */
  165.     return 0;
  166. } /* VAR_Dispatcher */
  167.  
  168.  
  169. /*
  170. **  DelAllVarsFromTree()
  171. **    recursively remove all nodes from a certain family
  172. **    and free them and their associated names&values
  173. */
  174.  
  175. void VAR_DelAll (VBASE *base)
  176. {
  177. #ifdef PATCH_AVL
  178.     if (!base || !(*base))
  179.     return;
  180.  
  181.     DelAllVarsFromTree(&(*base)->tn_Left);          /* first free its sons */
  182.     DelAllVarsFromTree(&(*base)->tn_Right);
  183.     ((VARS *)*base)->Parent = NULL;
  184.     VAR_Dispatcher ((VARS *)*base, (void *)&__DROP); /* then free it and its attrs */
  185. #else
  186.     VARS* v = NULL;
  187.  
  188.     if (base)
  189.     while ((v = (VARS*)RemHead((LIST*)base))) {         /* while there is an entry left in the list */
  190.         base->Parent = NULL;
  191.         VAR_Dispatcher ((VARS *)v, (void *)&__DROP);    /* free it and its attrs */
  192.     } /* while */
  193. #endif
  194. } /* VAR_DelAll */
  195.  
  196.  
  197.  
  198. /*
  199. **  VAR_SetNamed()
  200. **    define a node with a cetain name and value in
  201. **    a given family if there is already a node of the
  202. **    chosen name the previous node is removed
  203. **    (actually only the old value is removed)
  204. **    if any allocation went wrong, call nomemory()
  205. */
  206.  
  207. void VAR_SetNamed (VBASE *base, UBYTE *name, UBYTE *value)
  208. {
  209.     VARS *v;
  210.  
  211.     if (base)
  212.     if ((v = (VARS *)ANY_Find(base, name))) {
  213.         v->Str = strrep (v->Str, value);
  214.     } else {
  215.         if ((v = malloc(sizeof(VARS)))) {
  216.         v->Str    = strdup(value);
  217.         v->Name = strdup(name);
  218.  
  219.         if (v->Str && v->Name) {
  220.             v->Parent = (struct HNode *)base;
  221.             ANY_Add (base, &v->Node);
  222.             return;
  223.         } /* if malloced */
  224.         VAR_Dispatcher (v, (void *)&__DROP);
  225.         } /* if */
  226.         nomemory();
  227.     } /* if (no) old node */
  228. } /* VAR_SetNamed */
  229.  
  230.  
  231.  
  232.  
  233. /*
  234. **  VAR_DelNamed()
  235. **    remove a certain node (found w/ ANY_Find)
  236. **    from a given familiy; do not panik, if there is no
  237. **    node found
  238. */
  239.  
  240. void VAR_DelNamed (VBASE *base, UBYTE *name)
  241. {
  242.     VARS *v;
  243.  
  244.     if (base)
  245.     if ((v = (VARS *)ANY_Find(base, name)))     /* if there is a node of the right name */
  246.         VAR_Dispatcher (v, (void *)&__DROP);    /* then free it and its attrs */
  247. } /* VAR_DelNamed */
  248.  
  249.  
  250.  
  251.  
  252.  
  253. /*
  254. **  VAR_GetNamed()
  255. **    find a node in a certain family (w/ ANY_Find)
  256. **    and return its value ...
  257. **    return NULL if there was no node found
  258. */
  259.  
  260. UBYTE *VAR_GetNamed (VBASE *base, UBYTE *name)
  261. {
  262.     VARS *v;
  263.  
  264.     if (base)
  265.     if ((v = (VARS *)ANY_Find(base, name)))     /* if there is a node of the right name */
  266.         return v->Str;                /* return its value */
  267.     return NULL;
  268. } /* VAR_GetNamed */
  269.  
  270.  
  271. /******************************************************************************
  272. *****  END varsbases.c
  273. ******************************************************************************/
  274.  
  275.